﻿using WQLWebAPI.Application;
using WQLWebAPI.Domain.Abstract;
using WQLWebAPI.Mongodb;
using Autofac;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace WQLWebAPI.Api.Configuration
{
    public class AutofacModule : Autofac.Module
    {
        protected override void Load(ContainerBuilder builder)
        {

            // builder.Register(c => new SimulationService(c.Resolve<IConfiguration>())).InstancePerLifetimeScope();


            var Services = Assembly.Load("WQLWebAPI.Application");
            var IRepository = Assembly.Load("WQLWebAPI.Domain");
            var Repository = Assembly.Load("WQLWebAPI.Mongodb");

            //根据名称约定（服务层的接口和实现均以Services结尾），实现服务接口和服务实现的依赖
            builder.RegisterAssemblyTypes(Services)
              .Where(t => t.Name.EndsWith("Service"))
              .AsImplementedInterfaces();

            builder.RegisterAssemblyTypes(Repository, IRepository)
             .Where(t => t.Name.EndsWith("Repository"))
             .AsImplementedInterfaces();


            builder.RegisterGeneric(typeof(MongoBaseRepository<>)).As(typeof(IBaseRepository<>));



        }
    }
}
